<?php
class Image 
{
	public function ReturnImg ($imgsrc)
	{
		$src = str_replace(FANWE_ROOT,'/', str_replace('\\', '/', realpath($imgsrc)));
		
		return $src;
	}

	public function MakeThumb ($img,$thumb_width = 0,$thumb_height = 0)
	{
		$dir = date('Ym');
        $dir = FANWE_ROOT.'public/upload/goods/'.$dir.'/small/';
		
		$extArr= explode('.',$img);
		$extArr = array_reverse($extArr);
		
		if(count($extArr) == 0)
			$ext = ".jpg";
		else
		{
			$ext = ".".strtolower($extArr[0]);
			
			if($ext != ".jpg" && $ext != ".jpeg"  && $ext != ".gif" && $ext != ".giff" && $ext != ".png" && $ext != ".bmp")
				$ext = ".jpg";
		}
		
		$filename = uniqid();
        $filename = $dir . $filename . $ext;
		
		if (!file_exists($dir))
        {
            if (!makeDir($dir))
                return false;
        }
		
		$gd = $this->GDVersion();
		
		if ($gd == 0)
		{
			return $this->ReturnImg($img);
		}

		if ($thumb_width == 0 && $thumb_height == 0)
		{
			return $this->ReturnImg($img);
		}

		$org_info = @getimagesize($img);
		if (!$org_info)
		{
			return $this->ReturnImg($img);
		}
		if (!$this->CheckImgFunction($org_info[2]))
		{
			return $this->ReturnImg($img);
		}
		$img_org = $this->ImgResource($img, $org_info[2]);

		$scale_org = $org_info[0] / $org_info[1];

		if ($thumb_width == 0)
		{
			$thumb_width = $thumb_height * $scale_org;
		}
		if ($thumb_height == 0)
		{
			$thumb_height = $thumb_width / $scale_org;
		}

		if ($gd == 2)
		{
			$img_thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		}
		else
		{
			$img_thumb = imagecreate($thumb_width, $thumb_height);
		}
		$bgcolor = "FFFFFF";
		sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
		$clr = imagecolorallocate($img_thumb, $red, $green, $blue);
		imagefilledrectangle($img_thumb, 0, 0, $thumb_width, $thumb_height, $clr);
		if ($org_info[0] / $thumb_width < $org_info[1] / $thumb_height)
		{
			$lessen_width = $thumb_width;
			$lessen_height = $thumb_width / $scale_org;
		}
		else
		{
			$lessen_width = $thumb_height * $org_info[0] / $org_info[1];
			$lessen_height = $thumb_height;
		}
		$dst_x = ($thumb_width - $lessen_width) / 2;
		$dst_y = ($thumb_height - $lessen_height) / 2;

		if ($gd == 2)
			imagecopyresampled($img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info[0], $org_info[1]);
		else
			imagecopyresized($img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info[0], $org_info[1]);

		if (function_exists('imagejpeg'))
			imagejpeg($img_thumb, $filename, 100);
		elseif (function_exists('imagegif'))
			imagegif($img_thumb, $filename);
		elseif (function_exists('imagepng'))
			imagepng($img_thumb, $filename);
		else
			return $this->ReturnImg($img);
			
		imagedestroy($img_thumb);
		imagedestroy($img_org);
		if(file_exists($filename))
			return $this->ReturnImg($filename);
	}

	public function GDVersion ()
	{
		static $gdVersion = - 1;
		if ($gdVersion >= 0)
		{
			return $gdVersion;
		}
		if (! extension_loaded('gd'))
		{
			$gdVersion = 0;
		}
		else
		{
			if (PHP_VERSION >= '4.3')
			{
				if (function_exists('gd_info'))
				{
					$ver_info = gd_info();
					preg_match('/\d/', $ver_info['GD Version'], $match);
					$gdVersion = $match[0];
				}
				else
				{
					if (function_exists('imagecreatetruecolor'))
					{
						$gdVersion = 2;
					}
					elseif (function_exists('imagecreate'))
					{
						$gdVersion = 1;
					}
				}
			}
			else
			{
				if (preg_match('/phpinfo/', ini_get('disable_functions')))
				{
					$gdVersion = 1;
				}
				else
				{
					ob_start();
					phpinfo(8);
					$info = ob_get_contents();
					ob_end_clean();
					$info = stristr($info, 'gd version');
					preg_match('/\d/', $info, $match);
					$gdVersion = $match[0];
				}
			}
		}
		return $gdVersion;
	}

	public function CheckImgFunction ($img_type)
	{
		switch ($img_type)
		{
			case 'image/gif':
			case 1:
				if (PHP_VERSION >= '4.3')
				{
					return function_exists('imagecreatefromgif');
				}
				else
				{
					return (imagetypes() & IMG_GIF) > 0;
				}
				break;
			case 'image/pjpeg':
			case 'image/jpeg':
			case 2:
				if (PHP_VERSION >= '4.3')
				{
					return function_exists('imagecreatefromjpeg');
				}
				else
				{
					return (imagetypes() & IMG_JPG) > 0;
				}
				break;
			case 'image/x-png':
			case 'image/png':
			case 3:
				if (PHP_VERSION >= '4.3')
				{
					return function_exists('imagecreatefrompng');
				}
				else
				{
					return (imagetypes() & IMG_PNG) > 0;
				}
				break;
			default:
				return false;
		}
	}

	public function ImgResource ($img_file, $mime_type)
	{
		switch ($mime_type)
		{
			case 1:
			case 'image/gif':
				$res = imagecreatefromgif($img_file);
				break;
			case 2:
			case 'image/pjpeg':
			case 'image/jpeg':
				$res = imagecreatefromjpeg($img_file);
				break;
			case 3:
			case 'image/x-png':
			case 'image/png':
				$res = imagecreatefrompng($img_file);
				break;
			default:
				return false;
		}
		return $res;
	}
}
?>